import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_eails.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';

import 'kuan_eails_details_page.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/12/11.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class EamilsListPage extends StatefulWidget {
  @override
  _EamilsListPageState createState() => _EamilsListPageState();
}

class _EamilsListPageState extends State<EamilsListPage> {
  bool _isLoading = true;

  List<EailsBean> _list = [];

  @override
  void initState() {
    super.initState();
    getEamilsList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text("领导信箱"),
      ),
      backgroundColor: Colors.white,

      ///填充布局
      body: buildMain(),
    );
  }

  Future<bool> getEamilsList() async {
    //post请求发送
    ResponseInfo responseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.barmasterletterbox,
    );

    if (responseInfo.success) {
      _list = [];
      List list = responseInfo.data;
      list.forEach((element) {
        EailsBean newsBean = EailsBean.fromJson(element);
        _list.add(newsBean);
      });
    }

    _isLoading = false;
    if (!mounted) return true;

    setState(() {});

    return Future.value(true);
  }

  buildMain() {
    if (_isLoading) {
      return LoadingWidget();
    }
    if (_list.length == 0) {
      return NoDataWidget(
        clickCallBack: () {
          _isLoading = true;
          setState(() {});
          getEamilsList();
        },
      );
    }

    return RefreshIndicator(
      onRefresh: () async {
        getEamilsList();
        return true;
      },
      child: ListView.separated(
        itemCount: _list.length,
        itemBuilder: (BuildContext context, int index) {
          EailsBean eailsBean = _list[index];

          return InkWell(
            onTap: () {
              NavigatorUtils.pushPage(context, EamilsDetailsPage(eailsBean),
                  callback: (value) {
                setState(() {});
              });
            },
            child: eailsItemWidget(eailsBean),
          );
        },
        separatorBuilder: (BuildContext context, int index) {
          return Container(
            height: 1,
            color: Colors.grey[200],
          );
        },
      ),
    );
  }

  eailsItemWidget(EailsBean newsBean) {
    return Container(
      padding: EdgeInsets.all(10),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Row(
            children: [
              Text(
                "${newsBean.userEntity.reamName}",
                style: TextStyle(
                    fontSize: 16,
                    color: Colors.black,
                    fontWeight: FontWeight.w500),
              ),
              SizedBox(
                width: 10,
              ),
              Expanded(
                child: Text(
                  "(${newsBean.userEntity.username})",
                  style: TextStyle(
                      fontSize: 14,
                      color: Colors.black87,
                      fontWeight: FontWeight.normal),
                ),
              ),
              newsBean.isRead == 'Y'
                  ? Container()
                  : ClipOval(
                      child: Container(
                      width: 8,
                      height: 8,
                      color: Colors.red,
                    )),
            ],
          ),
          SizedBox(
            height: 8,
          ),
          Text(
            "${newsBean.content}",
            maxLines: 3,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
                fontSize: 14,
                color: Colors.grey[600],
                fontWeight: FontWeight.normal),
          ),
          SizedBox(
            height: 8,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              Text(
                "${newsBean.createTime}",
                style: TextStyle(
                    fontSize: 14,
                    color: Colors.grey[600],
                    fontWeight: FontWeight.normal),
              ),
            ],
          )
        ],
      ),
    );
  }
}
